package com.yuanthink.gwt.client.eventbus;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import com.extjs.gxt.ui.client.data.ModelData;

/**
 * 
 * @author 06peng
 * 
 */
@SuppressWarnings("rawtypes")
public class EventBus {
	
	Map registry;

	public EventBus() {
		registry = new HashMap();
	}

	@SuppressWarnings("unchecked")
	public void addHandler(String eventName, IAppEventHandler handler) {
		List handlers = (List) registry.get(eventName);
		if (handlers == null) {
			handlers = new Vector();
			registry.put(eventName, handlers);
		}
		handlers.add(handler);
	}

	public boolean fireEvent(String eventName, ModelData eventData) {
		List handlers = (List) registry.get(eventName);
		if (handlers == null) {
			return true;
		}
		for (int i = 0; i < handlers.size(); i++) {
			IAppEventHandler handler = (IAppEventHandler) handlers.get(i);
			if (handler.onEvent(eventName, eventData) == false) {
				return false;
			}
		}
		return true;
	}
}
